Option Strict On
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports Microsoft.WindowsLive.Id.Client
Imports System.Configuration




Public Class Form1

    Private oIDMgr As IdentityManager
    Private oID As Identity
    Public Shared currentUserName As String = ""
    Public currentBgColor As String = ""
    Public Shared defaultUserName As String = ""
    Public Shared tempBgColor As String = ""
    Public Shared tempDefaultUserName As String = ""



    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        Try
            oIDMgr = IdentityManager.CreateInstance("Tailspin Toys;user@tailspintoys.com;Tailspin Toys Application", "Windows Live ID Client Sample")


        Catch wlex As WLLogOnException
            'Check to see if FlowUrl is defined.
            If wlex.FlowUrl IsNot Nothing Then
                'If FlowUrl is defined, direct user to the web page to correct the error.
                MessageBox.Show(wlex.ErrorString + "Please go to " + wlex.FlowUrl.AbsoluteUri + "to correct the condition that caused the error")
            Else
                'If FlowUrl is not defined, simply display the ErrorString.
                MessageBox.Show(wlex.ErrorString)
            End If

        End Try

        'TrySilentSignIn()



    End Sub

    Private Sub TrySilentSignIn()
        'Try instantiating the global Identity object instance with the username from the config file.
        Try
            oID = oIDMgr.CreateIdentity(defaultUserName)
        Catch wlex As WLLogOnException
            'Check to see if FlowUrl is defined.
            If wlex.FlowUrl IsNot Nothing Then
                'If FlowUrl is defined, direct user to the web page to correct the error.
                MessageBox.Show(wlex.ErrorString + "Please go to " + wlex.FlowUrl.AbsoluteUri + "to correct the condition that caused the error")
            Else
                'If FlowUrl is not defined, simply display the ErrorString.
                MessageBox.Show(wlex.ErrorString)
            End If
        End Try

        'Check that the username is valid.
        If oID IsNot Nothing Then
            'Check to make sure the user has stored their username and password.
            If oID.SavedCredentials = CredentialType.UserNameAndPassword Then
                Try
                    'Try silent authentication.
                    If oID.Authenticate(AuthenticationType.Silent) Then
                        currentUserName = defaultUserName
                    Else
                        MessageBox.Show("Default user's stored sign-in name and password are invalid.")
                    End If
                Catch wlex As WLLogOnException
                    'Check to see if FlowUrl is defined.
                    If wlex.FlowUrl IsNot Nothing Then
                        'If FlowUrl is defined, direct user to the web page to correct the error.
                        MessageBox.Show(wlex.ErrorString + "Please go to " + wlex.FlowUrl.AbsoluteUri + "to correct the condition that caused the error")
                    Else
                        'If FlowUrl is not defined, simply display the ErrorString.
                        MessageBox.Show(wlex.ErrorString)
                    End If
                End Try
            Else
                MessageBox.Show("Default user hasn't stored sign-in name and password.")
            End If
        Else
            MessageBox.Show("defaultUserName in config file has an invalid value.")

        End If
    End Sub

    'Event handler for Sign-In/Sign-Out button clicks.
    Private Sub buttonSignInOrOut_Click(ByVal sender As Object, ByVal e As EventArgs) Handles buttonSignInOrOut.Click
        'Check to see if the user is not currently authenticated.
        oID = oIDMgr.CreateIdentity(defaultUserName)
        If Not oID.IsAuthenticated Then
            Try
                'Try to authenticate the user by showing the Sign-In dialog window.
                If oID.Authenticate() Then
                    currentUserName = oID.UserName
                Else
                    MessageBox.Show("Authentication failed")
                End If
            Catch wlex As WLLogOnException
                'Check to see if FlowUrl is defined.
                If wlex.FlowUrl IsNot Nothing Then
                    'If FlowUrl is defined, direct user to the web page to correct the error.
                    MessageBox.Show(wlex.ErrorString + "Please go to " + wlex.FlowUrl.AbsoluteUri + "to correct the condition that caused the error")
                Else
                    'If FlowUrl is not defined, simply display the ErrorString.
                    MessageBox.Show(wlex.ErrorString)
                End If
            End Try
        Else
            'If user is authenticated, they intended to Sign-Out. Try signing the user out.
            Try
                oID.CloseIdentityHandle()
                currentUserName = ""
            Catch wlex As WLLogOnException
                'Check to see if FlowUrl is defined.
                If wlex.FlowUrl IsNot Nothing Then
                    'If FlowUrl is defined, direct user to the web page to correct the error.
                    MessageBox.Show(wlex.ErrorString + "Please go to " + wlex.FlowUrl.AbsoluteUri + "to correct the condition that caused the error")
                Else
                    'If FlowUrl is not defined, simply display the ErrorString.
                    MessageBox.Show(wlex.ErrorString)
                End If
            End Try
        End If
        UpdateDisplay()
    End Sub

    Private Sub UpdateDisplay()
        'Method which updates the display of the application to reflect its current state.
        'Check to see if the user is authenticated and update the display appropriately.
        If oID.IsAuthenticated Then
            MessageBox.Show(oID.cId)
            toolStripStatusLabel1.Text = oID.UserName + " is Signed In"
            buttonSignInOrOut.Text = "Sign Out"
            'buttonOptions.Enabled = True
            'buttonBlog.Enabled = True
            'buttonDetails.Enabled = True
            'currentBgColor = ConfigurationManager.AppSettings(oID.cId + "_bgColor")
        Else
            'If user is not authenticated, update the display appropriately.
            toolStripStatusLabel1.Text = "Signed Out"
            buttonSignInOrOut.Text = "Sign In"
            'buttonOptions.Enabled = False
            'buttonBlog.Enabled = False
            'buttonDetails.Enabled = False
            currentBgColor = ""
        End If

        If Not [String].IsNullOrEmpty(currentBgColor) Then
            Me.BackColor = Color.FromArgb(Int32.Parse(currentBgColor))
        Else
            Me.BackColor = Form.DefaultBackColor
        End If
    End Sub




End Class
